//
//  CYImageView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/9/9.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYImageView.h"

@implementation CYImageView

- (instancetype)initWithImageName:(NSString *)image size:(CGSize)size isRadius:(BOOL)isRadius{
    self = [super init];
    if (self) {
        self.image = [UIImage imageNamed:image];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = (isRadius)?MIN(size.width/ 2.0, size.height / 2.0):0.0;
        self.bounds = CGRectMake(0.0, 0.0, size.width, size.height);
    }
    return self;
}

- (instancetype)initWithImage:(UIImage *)image size:(CGSize)size isRadius:(BOOL)isRadius{
    self = [super initWithImage:image];
    if (self) {
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = (isRadius)?MIN(size.width/ 2.0, size.height / 2.0):0.0;
        self.bounds = CGRectMake(0.0, 0.0, size.width, size.height);
    }
    return self;
}

- (instancetype)initWithImageName:(NSString *)image radius:(CGFloat)radius{
    self = [super init];
    if (self) {
        self.image = [UIImage imageNamed:image];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = radius;
    }
    return self;
}

//菊花
- (UIActivityIndicatorView *)loadingView{
    if (!_loadingView) {
        
        NSAssert((self.bounds.size.width > 0 || self.bounds.size.height > 0 ), @"初始化菊花之前，必须设置imageview的bounds");
        _loadingView = [[UIActivityIndicatorView alloc] initWithFrame:self.bounds];
        _loadingView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
        _loadingView.center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0);//设置菊花的中心点
        CGAffineTransform transform = CGAffineTransformMakeScale(0.5, 0.5);//设置菊花的大小
        _loadingView.transform = transform;
        
        [_loadingView startAnimating];//菊花开始动画
        self.backgroundColor = RGBA(245, 246, 250, 1);//设置imageview的背景颜色
        _loadingView.backgroundColor = [UIColor clearColor];//设置菊花的背景颜色
        _loadingView.color = [UIColor lightGrayColor];//设置菊花的颜色
        [self addSubview:_loadingView];
    }
    
    return _loadingView;
}
- (void)setImage:(UIImage *)image{
    [super setImage:image];
    if (image)
        _loadingView.hidden = YES;
}
@end
